﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using CloudBox.Helper.ExtensionMethod;
using CloudClip.ExtensionMethod;

namespace CloudBox.Feature.Clipboard.Implement
{
    public class TextClipUnit : IClipUnit
    {
        public TextClipUnit(string sText)
        {
            _ClipText = sText;
        }

        private string _ClipText = string.Empty;

        public string ToIntroduction()
        {
            const int iLength = 40;
            string sIntroductionText = _ClipText;
            sIntroductionText = Regex.Replace(sIntroductionText, "[\r\n\t]", " ", RegexOptions.IgnoreCase);
            sIntroductionText = Regex.Replace(sIntroductionText, "  +", " ", RegexOptions.IgnoreCase);
            sIntroductionText = sIntroductionText.Trim();

            if (sIntroductionText.Length < iLength)
            {
                return sIntroductionText;
            }
            else
            {
                return sIntroductionText.SubstringEx2(iLength).Trim() + "...";
            }
        }

        public bool Contain(object obj)
        {
            if (obj is TextClipUnit)
            {
                TextClipUnit unit = obj as TextClipUnit;

                if (_ClipText == unit.ToString())
                    return true;

                return false;
            }
            return false;
        }

        public string SetToFile(string sFileFolder)
        {
            //建立檔名
            string sFileName =
                Path.Combine(sFileFolder, MD5Helper.CreateMD5Hash(_ClipText).ToFileFormatString());

            using (StreamWriter streamWriter = new StreamWriter(sFileName))
            {
                streamWriter.Write(_ClipText);
            }

            return sFileName;
        }

        public void SetClipboard()
        {
            //Clipboard.SetDataObject(_ClipText);
        }

        public override string ToString()
        {
            return _ClipText;
        }
    }
}
